import 'package:flutter/material.dart';
import 'package:x3_base_package/x3_base_package.dart';
import 'package:x3_business_core/business_customer/ful_components/business_customer_details_service_sms_tab.dart';

class BusinessCustomerDetailsServiceSms extends StatefulWidget {
  static final String sName = 'BusinessCustomerDetailsServiceSms';

  @override
  _BusinessCustomerDetailsServiceSmsState createState() => _BusinessCustomerDetailsServiceSmsState();
}

class _BusinessCustomerDetailsServiceSmsState extends State<BusinessCustomerDetailsServiceSms> {
  dynamic _arguments;

  BaseTabBarPage _baseTabBarPage;

  @override
  void dispose() {
    _baseTabBarPage.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    _arguments = BaseRouterUtil.getArguments(context);
    String title = BaseUtils.getKeyData(_arguments, 'title').toString();
    return BaseScaffoldComponent(title: title, child: _buildMainScaffold());
  }

  Widget _buildMainScaffold() {
    return BaseScreenRefreshComponent(
      request: BaseScreenRequestModel('/vip/customer/server/tool/speakarttype', method: BaseHttpMethodEnum.GET),
      controller: BaseFutureController(),
      builder: (BaseHttpResultModel result) {
        if (result.data is List && (result.data as List).length == 0) return buildNoTemplate();
        return _buildTabPage(result.data);
      },
    );
  }

  Widget buildNoTemplate() {
    String vipId = BaseUtils.getKeyData(_arguments, 'vipId').toString();
    bool sms = _arguments['sms'] ?? true;
    bool privateWx = _arguments['privateWx'] ?? true;
    bool enterpriseWx = _arguments['enterpriseWx'] ?? false;
    return BusinessCustomerDetailsServiceSmsTab(inputApi: '', speakTypeId: '', vipId: vipId, noTemplate: true, sms: sms, privateWx: privateWx, enterpriseWx: enterpriseWx, phoneNumber: _arguments['phoneNumber'] ?? '');
  }

  Widget _buildTabPage(List<dynamic> items) {
    String inputApi = _arguments['inputApi'] ?? '';
    String vipId = _arguments['vipId'] ?? '';
    bool sms = _arguments['sms'] ?? true;
    bool privateWx = _arguments['privateWx'] ?? true;
    bool enterpriseWx = _arguments['enterpriseWx'] ?? false;
    String value = _arguments['value'] ?? '';
    int _initialIndex = items.indexWhere((element) {
      String _value = BaseUtils.getKeyData(element, 'value');
      return _value == value;
    });
    List<BaseTabConfig> _tabs = items
        .map((e) => BaseTabConfig(
            text: e['value'],
            child: BusinessCustomerDetailsServiceSmsTab(
              vipId: vipId,
              speakTypeId: e['key'],
              inputApi: inputApi,
              phoneNumber: _arguments['phoneNumber'] ?? '',
              sms: sms,
              privateWx: privateWx,
              enterpriseWx: enterpriseWx,
            )))
        .toList();
    _baseTabBarPage = BaseTabBarPage(_tabs, initialIndex: _initialIndex == -1 ? 0 : _initialIndex);
    return Column(
      children: [
        Container(
          width: double.infinity,
          height: 56.0,
          padding: EdgeInsets.symmetric(horizontal: 12.0, vertical: 4.0),
          alignment: Alignment.center,
          color: Colors.white,
          child: _baseTabBarPage.buildTabBar(context, isScrollable: true),
        ),
        Expanded(child: _baseTabBarPage.buildTabView()),
      ],
    );
  }
}
